const fs = require("fs");
const path = require('path');
const callfile = require('child_process');
const crypto = require('crypto');
var util = require('util');

let dataTypeConfig = [
    ["id", "number"],
    ["name", "string"],
    ["dengjiMoban", "number"],
    ["type", "string"],
    ["jichu", "number"],
    ["shanguangQ", "number"],
    ["shanguangH", "number"],
    ["shanguangcailiao", "number"],
    ["shanxiaohao", "number"],
    ["chongshengID", "number"],
    ["huhuan", "number"],
    ["chaojinhua", "string"],
    ["chaojinhuaHero", "number"],
    ["shifouChaojinhua", "number"],
    ["shifouShanguang", "number"],
    ["parent", "number"],
    ["child", "number"],
    ["jieduan", "number"],
    ["tiaojian", "number"],
    ["silver", "number"],
    ["leijigold", "number"],
    ["materia", "number"],
    ["count", "number"],
    ["leiji", "number"],
    ["rank", "number"],
    ["sssr", "number"],
    ["story", "string"],
    ["photo", "string"],
    ["face", "string"],
    ["avatar", "string"],
    ["hp", "number"],
    ["attack", "number"],
    ["defence", "number"],
    ["speed", "number"],
    ["sgType", "number"],
    ["sgValue", "number"],
    ["skill0", "number"],
    ["skill1", "number"],
    ["canGet", "number"],
    ["exp", "number"],
    ["talent1", "number"],
    ["talent2", "number"],
    ["talent3", "number"],
    ["talent4", "number"],
    ["talent5", "number"],
    ["talent6", "number"],
    ["suofang", "number"],
    ["canhunDefid", "number"],
    ["canhunNum", "number"],
    ["guanzhiMoban", "number"],
    ["chaojinhuaMoban", "number"],
    ["levelMoban", "number"],
    ["artifact", "number"],
    ["jump", "string"],
    ["gerenkeji", "number"],
    ["shenqiEXP", "number"],
    ["paihangjifen", "number"],
    ["chengzhang", "number"],
    ["guanzhiMax", "number"],
    ["zhongzumoban", "number"],
    ["xilianLimit", "number"],
    ["shidai", "number"],
    ["exhibition", "number"],
    ["exhibitionLevel", "number"],
]
// let buffer = new ArrayBuffer((1 << 20) * 10);
let binPath = path.join(__dirname, "../", 'dest', 'WujiangDef.bin')
let buffer = fs.readFileSync(binPath);


// let view = new DataView(buffer);
let view = buffer
let offSet = 0
var lengthConfig = {
    ["Int32"]: 8,
    // ['setInt16']: 4,
}
console.log("buffInfo:", Buffer.from(buffer, [, 0, [4]]))
let wujiangLength = view.readInt32BE(0) //数据长度
console.log("wujiangLength", wujiangLength)
offSet += lengthConfig.Int32
//先通过一个32位int来表示某一位是否有值
let defaultVueMap = {
    "number": 0,
    'string': '',
}

const encoder = new util.TextEncoder();
const decoder = new util.TextDecoder();
let decodeFunction = {
    "number": (view) => {
        let vue = view.readInt32BE(offSet)
        offSet += lengthConfig.Int32
        return vue
    },
    'string': (view) => {
        let length = view.readInt32BE(offSet)
        offSet += lengthConfig.Int32
        // for (let char of vue) {
        //     view.setUint8(offSet, char)
        //     offSet += 1
        // }
        let array = new Uint8Array(buffer.buffer, offSet, length)
        vue = decoder.decode(array)
        offSet += length
        return vue
    },
}
function decodeObj(view, typeConfig) {
    //写入位置
    // let vue = 0
    // for (let config of typeConfig) {
    //     vue = vue << 1 + (obj[config[0]] == defaultVueMap[config[1]] ? 0 : 1)
    // }
    // view.setInt32(vue)
    // let signVue = view.getInt32(offSet)
    // offSet += lengthConfig.Int32
    let length = Math.ceil(typeConfig.length / 8)
    let signStr = ""
    let array = new Uint8Array(buffer.buffer, offSet, length)
    for (let signInt of array) {
        let str = signInt.toString(2)
        if (str.length < 8) {
            for (let i = str.length; i < 8; i++) {
                signStr += '0' //补空
            }
        }
        signStr += signInt.toString(2)
    }
    offSet += length
    //写入真实数据
    let resObj = {}
    typeConfig.forEach((config, idx) => {
        // signVue >> (typeConfig.length - idx + 1) % 2
        if (!signStr[idx]) {
            resObj[config[0]] = defaultVueMap[config[1]]
        } else {
            resObj[config[0]] = decodeFunction[config[1]](view)
        }
    });
    return resObj
}
let arrayList = []
for (let i = 0; i < wujiangLength; i++) {
    arrayList.push(decodeObj(view, dataTypeConfig))
}
// for (let obj of wujiangConfig) {
//     writeObj(view, obj, dataTypeConfig)
// }
let destPath = path.join(__dirname, "../", 'dest', 'WujiangDefTest.json')
// let viewOut = new DataView(buffer, 0, offSet)
fs.writeFileSync(destPath, JSON.stringify(arrayList))
